<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/修改装备品阶</title>
<%@ include file="/view/common/tags.jsp"%>
</head>
<body>
	<div class="well well_p" >
		<sk:form method="post" cssClass="form-horizontal" id="saveAndUpdateFrom" target="_parent" action="">
			<input type="hidden" id="rowId" name="rowId" value='<%=request.getParameter("rowId")%>'/>
			<input type="hidden" id="isReCommit" value="0">
			<input type="hidden" id="token" name="token" value="">
			<input type="hidden" id="gameId" name="gameId" value="">
			<input type="hidden" id="gameName" name="gameName" value="">
			<input type="hidden" id="currentEquipQualityId" name="currentEquipQualityId" value="">
			<input type="hidden" id="currentEquipQualityLevel" name="currentEquipQualityLevel" value="">
			<input type="hidden" id="targetEquipQualityId" name="targetEquipQualityId" value="">
			<input type="hidden" id="targetEquipQualityLevel" name="targetEquipQualityLevel" value="">
			<div class="form-group">
				<label class="col-xs-3 text-right">游戏名称:</label>
				<div class="col-xs-5">
					<sk:select cssClass="form-control select" id="gameIdSelect" name="gameIdSelect" value="" requiredInit="true" ajaxURL="${userInfoV1}/baseGame/gameSelect?wm[DEL_FLAG]=1" onchange="TagUtil.fillOptions('gameIdSelect')">
						<option value="">请选择</option>
					</sk:select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">当前装备品阶:</label>
				<div class="col-xs-5">
					<sk:select cssClass="form-control select" id="currentEquipQualityLevelSelect" name="currentEquipQualityLevelSelect" value="" requiredInit="true" parentCascadedSelect="gameIdSelect" ajaxURL="${userInfoV1}/gameEquipLevel/gameEquipLevelSelect?1=1">
						<option value="">请选择</option>
					</sk:select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">目标装备品阶:</label>
				<div class="col-xs-5">
					<sk:select cssClass="form-control select" id="targetEquipQualityLevelSelect" name="targetEquipQualityLevelSelect" value="" requiredInit="true" parentCascadedSelect="gameIdSelect" ajaxURL="${userInfoV1}/gameEquipLevel/gameEquipLevelSelect?1=1">
						<option value="">请选择</option>
					</sk:select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">所需物品：</label>
				<div class="col-xs-7">
					<textarea id="requiredGoods" name="requiredGoods" style="width:300px;height:200px;border:#ccc solid 1px;text-indent:3px;padding-top:3px;"/></textarea>
				</div>
			</div>
			<div class="form-group" >
				<label class="col-xs-3 text-right">是否开启:</label>
				<div class="col-xs-7">
					<td colspan="2">
						<label><input name="openFlag" type="radio" checked="checked"  value="1"/>&nbsp;是</label>&nbsp;&nbsp;&nbsp;&nbsp;
						<label><input name="openFlag" type="radio"  value="0"/>&nbsp;否</label>
					</td>
				</div>
			</div>
		</sk:form>
	</div>
</body>
<%@ include file="/view/common/footer.jsp"%>
<script>
    var gameEquipLevelRelationId = $("#rowId").val();//装备品阶关系ID
    $(function(){
        //添加修改装备品阶关系时根据ID控制
        if (CheckUtil.isNotNull(gameEquipLevelRelationId) && parseInt(gameEquipLevelRelationId) > 0) {
            $('#gameIdSelect').attr("disabled", "true");
        }
        //后台登录用户token
        $("#token").val(sessionStorage.getItem("token"));
        //修改装备品阶关系时获取数据
        if(gameEquipLevelRelationId > 0){
            $.ajax({
                url: "${userInfoV1}/gameEquipLevelRelation/getGameEquipLevelRelationInfo",
                type: "post",
                cache: false,
                data:{"rowId":gameEquipLevelRelationId},
                dataType:"json",
                async:false,
                success: function(data) {
                    if (data == null) {
                        return;
                    }
                    $("#gameId").val(data.gameId);
                    $("#gameName").val(data.gameName);
                    $("#gameIdSelect").val(data.gameId);
                    TagUtil.fillOptions('gameIdSelect');
                    $("#currentEquipQualityId").val(data.currentEquipQualityId);
                    $("#currentEquipQualityLevel").val(data.currentEquipQualityLevel);
                    $("#currentEquipQualityLevelSelect").val(data.currentEquipQualityId);
                    $("#targetEquipQualityId").val(data.targetEquipQualityId);
                    $("#targetEquipQualityLevel").val(data.targetEquipQualityLevel);
                    $("#targetEquipQualityLevelSelect").val(data.targetEquipQualityId);
                    $("#requiredGoods").val(data.requiredGoods);
                    $("input[name='openFlag']").each(function(){
                        if (data.openFlag == $(this).val()) {
                            $(this).attr("checked",true);
                        } else {
                            $(this).attr("checked",false);
                        }
                    });
                }
            });
        }
    });
    //校验装备品阶关系是否重复
    function checkGameEquipLevelRelation(operaType) {
        var currentEquipQualityId = $("#currentEquipQualityLevelSelect").val();
        var targetEquipQualityId = $("#targetEquipQualityLevelSelect").val();
        var gameId = $("#gameIdSelect").val();
        var rowId = $("#rowId").val();
        if (!CheckUtil.isNotNull(gameId)) {
            layer.msg("请选择游戏!");
            return;
        }
        if (!CheckUtil.isNotNull(currentEquipQualityId)) {
            layer.msg("请选择当前装备品阶!");
            return;
        }
        if (!CheckUtil.isNotNull(targetEquipQualityId)) {
            layer.msg("请选择目标装备品阶!");
            return;
        }
        if (CheckUtil.isNotNull(currentEquipQualityId) && CheckUtil.isNotNull(targetEquipQualityId) && CheckUtil.isNotNull(gameId)) {
            $.ajax({
                type: "post",
                cache: false,
                url: "${userInfoV1}/gameEquipLevelRelation/checkGameEquipLevelRelation",
                data:{currentEquipQualityId:currentEquipQualityId, targetEquipQualityId:targetEquipQualityId, rowId:rowId, gameId:gameId},
                dataType:"json",
                async:false,
                success: function(data) {
                    result = data;
                    if (data == true || data == "true") {
                        layer.msg("装备品阶关系重复");
                        $("#raiderType").focus();
                    } else if (operaType == "1") {
                        addAfterCheck();
                    }
                }
            });
        }
    }
    var err = {'1':'添加','2':'修改'};
    var action = {'1': '${userInfoV1}/gameEquipLevelRelation/save','2': '${userInfoV1}/gameEquipLevelRelation/update'};
    function add(){
        //检查装备品阶关系是否重复
        checkGameEquipLevelRelation("1");
    }
    //装备品阶关系不重复则执行
    function addAfterCheck() {
        var rowId = $('#rowId').val();
        var opera;
        if (!CheckUtil.isNotNull(rowId) || rowId <= 0) {
            opera = 1;//添加
        } else {
            opera = 2;//修改
        }
        var gameId = $("#gameIdSelect").val();
        if (!CheckUtil.isNotNull(gameId)) {
            layer.msg("请选择游戏!");
            return;
        }
        var currentEquipQualityId = $("#currentEquipQualityLevelSelect").val();
        if (!CheckUtil.isNotNull(currentEquipQualityId)) {
            layer.msg("请选择当前装备品阶!");
            return;
        }
        var targetEquipQualityId = $("#targetEquipQualityLevelSelect").val();
        if (!CheckUtil.isNotNull(targetEquipQualityId)) {
            layer.msg("请选择目标装备品阶!");
            return;
        }
        var requiredGoods = $("#requiredGoods").val();
        if (!CheckUtil.isNotNull(requiredGoods)) {
            layer.msg("所需物品不能为空!");
            return;
        }
        var openFlag =$('input:radio[name="openFlag"]:checked').val();
        if (!CheckUtil.isNotNull(openFlag)) {
            layer.msg("请选择是否开启!");
            return;
        }
        if ($("#isReCommit").val() == 0) {
            $("#isReCommit").val(1);
            //loading层
            var index = layer.load(0, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $("#gameId").val($("#gameIdSelect").val());
            $("#gameName").val($("#gameIdSelect option:selected").text());
            $("#currentEquipQualityId").val($("#currentEquipQualityLevelSelect").val());
            $("#currentEquipQualityLevel").val($("#currentEquipQualityLevelSelect option:selected").text());
            $("#targetEquipQualityId").val($("#targetEquipQualityLevelSelect").val());
            $("#targetEquipQualityLevel").val($("#targetEquipQualityLevelSelect option:selected").text());
            $.ajax({
                url: action[opera],
                type: "post",
                cache: false,
                //data:parseJson(decodeURIComponent($("#saveAndUpdateFrom").serialize())),
                data: new FormData($("#saveAndUpdateFrom")[0]),
                dataType:"json",
                processData: false,
                contentType: false,
                async:false,
                success: function(data) {
                    layer.close(index);
                    if(data.code == 0){
                        parent.layer.msg("添加成功！");
                    } else if(data.code == 1){
                        parent.layer.msg("修改成功！");
                    } else if(data.code == "-2"){
                        parent.window.location.href="${sysManagerV1}/";
                    } else {
                        parent.layer.msg(data.message);
                    }
                    if(data.code >= 0){
                        parent.findData();
                        parent.layer.close(parent.tableIndex);
                    }
                }
            });
        }
    }
</script>
</html>